package com.sharekitchen.controller;

import com.sharekitchen.common.R;
import com.sharekitchen.dto.DeviceDto;
import com.sharekitchen.dto.RoomDto;
import com.sharekitchen.entity.*;
import com.sharekitchen.service.DeviceService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

@Slf4j
@RestController
@RequestMapping("/device")
public class DeviceController {
    @Autowired
    private DeviceService deviceService;

    @PostMapping("/all")
    public R<List<DeviceDto>> getAllDevices(@RequestBody Page page) {
        HashMap<String, DeviceDto> hashMap = new HashMap<>();
        List<Device> allDev = deviceService.getAllDev(page);
        if (allDev == null) {
            return R.fail("查询失败,不存在该设备");
        }
        for (Device device :allDev) {
            DeviceDto deviceDto = new DeviceDto();
            deviceDto.setDevId(device.getDevId());
//            获取相对应dev的devDetail
            DeviceDetail devDetail = deviceService.getDevDetailById(device.getDevId());
            if (devDetail == null){
                return R.fail("查询失败,不存在该设备");
            }
            //            获取相对应dev的img图像
            DeviceImages devImgById = deviceService.getDevImgById(device.getDevId());
            if (devDetail == null){
                return R.fail("查询失败,不存在该设备");
            }
            deviceDto.setDeviceImages(devImgById);
            deviceDto.setDeviceDetail(devDetail);
            deviceDto.setDevName(device.getDevName());
            deviceDto.setDevDesc(device.getDevDesc());
            deviceDto.setIsDelete(device.getIsDelete());
            hashMap.put(deviceDto.getDevId(), deviceDto);
        }

        List<DeviceDto> deviceDtos = new ArrayList<>(hashMap.values());
        if (deviceDtos != null) {
            return R.ok("查询成功", deviceDtos);
        }
        return R.error("查询失败");
    }

    @GetMapping("/id")
    public R<DeviceDto> getDevById(@RequestParam String devId) {

        DeviceDto deviceDto = new DeviceDto();
        Device devById = deviceService.getDevById(devId);
        if (devById == null) {
            return R.fail("查询失败,不存在该设备");
        }
        DeviceImages devImgById = deviceService.getDevImgById(devId);
        if (devImgById == null) {
            return R.fail("查询失败,不存在该设备");
        }
        DeviceDetail devDetailById = deviceService.getDevDetailById(devId);
        if (devDetailById == null) {
            return R.fail("查询失败,不存在该设备");
        }
        deviceDto.setDeviceImages(devImgById);
        deviceDto.setDeviceDetail(devDetailById);
        deviceDto.setDevId(devById.getDevId());
        deviceDto.setDevDesc(devById.getDevDesc());
        deviceDto.setDevName(devById.getDevName());
        deviceDto.setIsDelete(devById.getIsDelete());

        if (deviceDto != null) {
            return R.ok("查询成功", deviceDto);
        }

        return R.error("查询失败");
    }

    @PostMapping("/add")
    public R<String> addDevice(@RequestBody DeviceDto deviceDto) {
        String devId = UUID.randomUUID().toString().replace("-", "").substring(0, 20);
        deviceDto.setDevId(devId);
        DeviceDetail deviceDetail = deviceDto.getDeviceDetail();
        deviceDetail.setDevId(devId);

        DeviceImages deviceImages = deviceDto.getDeviceImages();
        deviceImages.setDevId(devId);

        if (!deviceService.addDeviceDetail(deviceDetail)) {
            return R.fail("添加失败");
        }
        if (!deviceService.addDeviceImages(deviceImages)) {
            return R.fail("添加失败");
        }
        if (!deviceService.addDevice(deviceDto)) {
            return R.fail("添加失败");
        }

        return R.ok("添加成功", null);
    }

    @DeleteMapping("/delete")
    public R<String> deleteDeviceById(@RequestParam String devId) {
        if (!deviceService.deleteDeviceImages(devId)) {
            return R.fail("删除失败,没有该设备");
        }

        if (!deviceService.deleteDeviceDetail(devId)) {
            return R.fail("删除失败,没有该设备");
        }

        if (deviceService.deleteDevice(devId)) {
            return R.ok("删除成功", null);
        }

        return R.error("删除失败");
    }

    @PutMapping("/change")
    public R<String> updateDevice(@RequestBody DeviceDto deviceDto) {
        DeviceDetail deviceDetail = deviceDto.getDeviceDetail();
        DeviceImages deviceImages = deviceDto.getDeviceImages();
        if (!deviceService.updateDeviceDetail(deviceDetail)) {
            return R.fail("设备Detail修改失败");
        }
        if (!deviceService.updateDeviceImages(deviceImages)) {
            return R.fail("设备图片修改失败");
        }

        if (deviceService.updateDevice(deviceDto)) {
            return R.ok("修改成功", null);
        }
        return R.error("修改失败");
    }

    @GetMapping("/total")
    public R<Integer> getTotalCount(){
        Integer totalCount = deviceService.getTotalCount();
        if (totalCount >= 1){
            return R.ok("查询成功",totalCount);
        }
        if (totalCount == 0){
            return R.fail("没有任何设备");
        }
        return R.error("查询错误");
    }

    @GetMapping("/num")
    public R<List<Long>> getDeviceNum(){
        List<Long> deviceNum = deviceService.getDeviceNum();
        if (deviceNum!=null){
            return R.ok("查询成功",deviceNum);
        }
        return R.fail("查询失败");
    }

}
